/**
 * 
 */
package org.algos.tree;

/**
 * 
 * Interval class holds the [ start : end ]
 * values.
 * 
 * @author Sreehari
 *
 */
public class Interval<T extends Comparable<T>> {

	/**
	 * Starting value of the interval
	 */
	private T start;
	
	/**
	 * Ending value of the interval
	 */
	private T end;
	
	public Interval(T start, T end) {
		this.start = start;
		this.end   = end;
	}
	
	public T getStart() {
		return start;
	}

	public void setStart(T start) {
		this.start = start;
	}


	public T getEnd() {
		return end;
	}


	public void setEnd(T end) {
		this.end = end;
	}

	/**
	 * This method tests if the two intervals overlap
	 * or not
	 * current[start] <= other[end] and
	 * current[end] >= other[start]
	 * 
	 * @param other
	 * @return
	 */
	public boolean overlapWith(Interval<T> other){
		return (start.compareTo(other.getEnd()) <= 0) && 
				(end.compareTo(other.getStart()) >= 0);
	}
	
	@Override
	public String toString() {
		return "["+start.toString()+":"+end.toString()+"]";
	}
	
	@Override
	public int hashCode() {
		return start.hashCode() * 17 + end.hashCode() * 13; 
	}
	
	@Override
	/**
	 * Two intervals are equal only if both the start & end
	 * are equal
	 */
	public boolean equals(Object obj) {
		
		if(obj instanceof Interval){
			Interval<T> o = (Interval<T>)obj;
			
			return (start.equals(o.getStart())) && (end.equals(o.getEnd()));
		}
		return false;
	}
}
